package com.hongyuan.venue.ui.home.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.venue.R;
import com.hongyuan.venue.base.CustomViewModel;
import com.hongyuan.venue.base.ItemViewModel;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.MyRepository;
import com.hongyuan.venue.data.md5.BaseUtil;
import com.hongyuan.venue.ui.home.activity.VenuePayActivity;
import com.hongyuan.venue.ui.home.beans.SecheduleBeans;
import com.hongyuan.venue.ui.home.beans.TopTimeBean;
import com.hongyuan.venue.utils.BigDecimalUtils;
import com.hongyuan.venue.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class ScheduledViewModel extends CustomViewModel<MyRepository> {

    //选中的总费用
    public ObservableField<String> allPrice = new ObservableField<>("0");
    //选中的总费用
    public ObservableField<List<SecheduleBeans.ListBean>> selectAllBean = new ObservableField<>(new ArrayList<>());

    //选中项
    public ObservableField<TopTimeBean> selectBean = new ObservableField<>();

    //给RecyclerView添加ObservableList
    public ObservableList<TimeTopItemViewModel> topList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<TimeTopItemViewModel> topBinding = ItemBinding.of(BR.viewModel, R.layout.item_home_top);

    public ScheduledViewModel(@NonNull Application application, MyRepository repository) {
        super(application, repository);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //显示表格数据
        public SingleLiveEvent<SecheduleBeans> showContents = new SingleLiveEvent<>();
    }

    //确认订单
    public BindingCommand goSubmit = new BindingCommand(() -> {

        if(selectAllBean.get().size() > 0){
            String detail = GsonUtil.toJsonStr(selectAllBean.get());
            Bundle bundle = new Bundle();
            bundle.putString("detail",detail);
            bundle.putString("allPrice",allPrice.get());
            startActivity(VenuePayActivity.class,bundle);
        }else{
            ToastUtils.showShort("请选择！");
        }

    });

    /*
    * 改变当前选中的总费用
    * */
    public void changeAllPrice(String price){
        allPrice.set(BaseUtil.getNoZoon(BigDecimalUtils.add(allPrice.get(),price,2)));
    }


    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_PLAN_DATE){
            List<TopTimeBean> mList = (List<TopTimeBean>) dataBean;

            topList.clear();

            for(int i = 0 ; i < mList.size() ; i++){
                TimeTopItemViewModel itemViewModel = new TimeTopItemViewModel(this,mList.get(i));
                topList.add(itemViewModel);

                if(i == 0){
                    mList.get(i).setSelect(true);
                    selectBean.set(mList.get(i));

                    this.itemViewModel = itemViewModel;
                }
            }

            //请求第一天的预约列表
            setParams("os_id",userToken.getOs_id())
                    .setParams("date",mList.get(0).getNow_day())
                    .requestData(Constants.GYM_RESERVE_LIST);
        }

        if(code == Constants.GYM_RESERVE_LIST){
            itemViewModel.entity.get().setSelect(true);
            itemViewModel.entity.notifyChange();

            SecheduleBeans beans = (SecheduleBeans)dataBean;
            uc.showContents.setValue(beans);
        }

    }


    //当前操作的对象
    public TimeTopItemViewModel itemViewModel;
    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);
        this.itemViewModel = (TimeTopItemViewModel) itemVM;
        selectBean.set(itemViewModel.entity.get());
    }
}
